#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/07/11
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import Blueprint, render_template, redirect, url_for, request, json
from flask_login import login_required, current_user
from flasky.common import manager_required, app_logger
from .axios import AxiosManager


# 车场管理
parking = Blueprint(name='parking', import_name='flasky', url_prefix='/parking', template_folder='views/templates')
ParkingManager= AxiosManager()


@parking.route('/')
@login_required
@manager_required
def default():
    """
    车场设置
    """
    return redirect(url_for('parking.setting'))


@parking.route('/setting', methods=['POST', 'GET'])
@login_required
@manager_required
def setting():
    """
    车场设置
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'setting'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：parking.setting 数据：{p_data}")
            return ParkingManager.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：parking.setting 数据：{p_data} 错误：{e}")
        return ParkingManager.error('處理錯誤，請聯係管理員')
    return render_template('parking/setting.html', **ParkingManager.setting_page_data())


@parking.route('/places', methods=['POST', 'GET'])
@login_required
@manager_required
def places():
    """
    车位管理
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'places'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：parking.places 数据：{p_data}")
            return ParkingManager.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：parking.places 数据：{p_data} 错误：{e}")
        return ParkingManager.error('處理錯誤，請聯係管理員')
    return render_template('parking/places.html', **ParkingManager.parking_place_page_data())


@parking.route('/cashers')
@login_required
@manager_required
def cashers():
    """
    收费员管理
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'cashers'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：parking.cashers 数据：{p_data}")
            return ParkingManager.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：parking.cashers 数据：{p_data} 错误：{e}")
        return ParkingManager.error('處理錯誤，請聯係管理員')
    return render_template('parking/cashers.html', **ParkingManager.cashers_page_data())


@parking.route('/rule')
@login_required
@manager_required
def rule():
    """
    收费规则
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'rule'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：parking.rule 数据：{p_data}")
            return ParkingManager.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：parking.rule 数据：{p_data} 错误：{e}")
        return ParkingManager.error('處理錯誤，請聯係管理員')
    return render_template('parking/rule.html', **ParkingManager.rule_page_data())
